<template>
  <div class="mask">
    <transition
      enter-active-class="animated fadeInDown"
      leave-active-class="animated fadeOutUp"
    >
      <div class="modal-content" v-if="visible">
        <TempComponent
          @close="close"
          :params="params"
          @success="handleSuccess"
        ></TempComponent>
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  props: {
    template: null,
    params: {
      type: Object,
      default: () => {},
    },
    success: {
      type: Function,
      default: () => {},
    },
    fail: {
      type: Function,
      default: () => {},
    },
  },
  data() {
    return {
      visible: false,
    };
  },
  components: {
    TempComponent: null,
  },
  mounted() {
    this.createElement();
  },
  beforeDestroy() {
    this.$el.parentNode.removeChild(this.$el);
  },
  methods: {
    createElement() {
      this.visible = true;
      document.body.appendChild(this.$el);
    },
    close() {
      this.visible = false;
    },
    destoryElement() {
      this.$destroy();
    },
    handleSuccess(data) {
      this.success(data);
      this.close()
    },
    handleFail(data) {
      this.fail(data);
      this.close()
    },
  },
  watch: {
    visible(newVal) {
      if (!newVal) {
        this.$el.addEventListener("animationend", this.destoryElement, false);
      }
    },
  },
};
</script>

<style  lang="scss" scoped>
@import "../../style/common.scss";
.mask {
  @include mask();
}

.modal-content {
  background: #fff;
  width: 50%;
  padding: 20px;
}
</style>